<template>
  <div class="review-container">
	  <button class="back-btn" @click="goBack">← 返回上一页</button>
    <!-- 评论列表 -->
    <h2 class="title">用户评论（共{{ reviews.length }}条）</h2>
    <div v-if="reviews.length === 0" class="empty-tip">暂无评论，快来发表第一条评论吧~</div>
    <ul v-else class="review-list">
      <li v-for="review in reviews" :key="review.reviewId" class="review-item">
        <div class="user-info">
          <span class="username">{{ review.username }}</span>
          
        </div>
        <div class="review-content">{{ review.content }}</div>
        <button 
          v-if="review.userId === user.userId"
          class="delete-btn"
          @click="handleDelete(review.reviewId)"
        >
          <i class="iconfont icon-delete"></i>
        </button>
      </li>
    </ul>

    <!-- 发表评论 -->
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'FoodReview',
  data() {
    return {
      reviews: [],      // 评论列表
      newReview: '',    // 新评论
      user: {},         // 用户信息
      foodId: null      // 食物ID
    };
  },
  created() {
    this.foodId = this.$route.query.foodId;
    this.checkLogin();
    this.loadReviews();
  },
  methods: {
    // 登录验证（保持不变）
    checkLogin() { 
      /* 登录验证的逻辑 */
    }, goBack() {
    this.$router.go(-1) // 路由回退
  },

    // 加载评论
    
	async  loadReviews() {
	  try {
	    let response = await  this.$axios.get('/Review/ReviewListOfFood', {
        params: { foodId: this.foodId }
      })
	    this.reviews = response.data.result;
	
	    // 获取每个订单的地址
	    for (let review of this.reviews) {
	      
		  review.username=await this.getname(review.userId);
	    }
	  } catch (error) {
	    
	  }
	},
	async getname(userId) {
	  try {
	    let response = await this.$axios.get('/User/UserName', {
	      params: { userId }
	    });
	    return response.data.result;
	  } catch (error) {
	    console.error('获取评论失败', error);
	    return '获取评论失败';
	  }
	},


  }
};
</script>

<style scoped>
	.back-btn {
	  padding: 8px 16px;
	  margin-bottom: 20px;
	  background: #f5f5f5;
	  color: #666;
	  border: 1px solid #ddd;
	  border-radius: 4px;
	  cursor: pointer;
	  transition: all 0.3s;
	}
	
	.back-btn:hover {
	  background: #2d8cf0;
	  color: white;
	  border-color: #2d8cf0;
	  transform: translateY(-1px);
	  box-shadow: 0 2px 8px rgba(45, 140, 240, 0.2);
	}
.review-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}

.title {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #eee;
}

/* 评论列表 */
.review-list {
  margin: 30px 0;
}

.review-item {
  position: relative;
  padding: 20px;
  margin-bottom: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s;

  &:hover {
    transform: translateX(10px);
    box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
  }
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;

  .username {
    font-weight: 500;
    color: #2d8cf0;
    margin-right: 15px;
  }

  .time {
    font-size: 12px;
    color: #999;
  }
}

.review-content {
  line-height: 1.6;
  color: #666;
}


</style>
